﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DoAn.DTO;
using System.Security.Cryptography;
using System.Text;
using System.Data;

namespace DoAn.DAO
{
    public class TaiKhoanDAO
    {
        public static void dangKiTK(TaiKhoanDTO taik)
        {
            string sql = "insert into TAIKHOAN(TenTaiKhoan,MatKhau,LoaiTaiKhoan,HoTen,NgaySinh,DiaChi,SoCMND,Email,MaKichHoatTK,TinhTrangKichHoatTK,LoaiTheNganHang,TenChuThe,MaThe,NgayHetHan,TinhTrangThe)  values('"
            +taik.TenTaiKhoan+"','"+taik.MatKhau+"','"+taik.LoaiTaiKhoan.ToString()+"',N'"+taik.HoTen+"','"+taik.NgaySinh.ToShortDateString()+
            "',N'"+taik.DiaChi+"','"+taik.SoCMND+"','"+taik.Email+"','"+taik.MaKichHoatTK+"','"+taik.TinhTrangKichHoatTK.ToString()+"','"+taik.LoaiTheNganHang+
            "',N'"+taik.TenChuThe+"','"+taik.MaThe+"','"+taik.NgayHetHan.ToShortDateString()+"','"+taik.TinhTrangThe.ToString()+"')";
            DataProvider.ExcuteQuery(sql);
        }
        public static bool kichHoatTaiKhoan(TaiKhoanDTO tk)
        {
            string sql = "select * from TAIKHOAN where TenTaiKhoan='" + tk.TenTaiKhoan + "' and MaKichHoatTK='" + tk.MaKichHoatTK + "' and TinhTrangKichHoatTK='False'";
            DataTable dt = new DataTable();
            dt = DataProvider.DataTable(sql);
            if (dt.Rows.Count > 0)
            {
                sql = "update TAIKHOAN set TinhTrangKichHoatTK='True' where TenTaiKhoan='" + tk.TenTaiKhoan + "'";
                DataProvider.ExcuteQuery(sql);
                return true;
            }
            else
            {
                return false;
            }
        }
        public static DataTable dangNhap(TaiKhoanDTO tk)
        {
            string sql = "select * from TAIKHOAN where TenTaiKhoan='"+tk.TenTaiKhoan+"' and MatKhau='"+tk.MatKhau+"'";
            DataTable dt = new DataTable();
            dt=DataProvider.DataTable(sql);
            return dt;
        }
        public static bool isLogged()
        {
            return HttpContext.Current.Request.Cookies["TenTaiKhoan"] != null;
        }
        public static string tenTaiKhoan()
        {
            return HttpContext.Current.Request.Cookies["TenTaiKhoan"].Value;
        }
        public static string loaiTaiKhoan()
        {
            return HttpContext.Current.Request.Cookies["LoaiTaiKhoan"].Value;
        }

        public static DataTable loadTaiKhoan(TaiKhoanDTO tk)
        {
            string sql = "Select * from TaiKhoan where TenTaiKhoan='" + tk.TenTaiKhoan + "'";
            DataTable dt = new DataTable();
            dt = DataProvider.DataTable(sql);
            return dt;
        }

        public static void thayDoiEmail(TaiKhoanDTO tk)
        {
            string sql = "update Taikhoan set Email='" + tk.Email + "' where TenTaiKhoan='" + tk.TenTaiKhoan + "'";
            DataProvider.ExcuteQuery(sql);
        }

        public static void doiMatKhau(TaiKhoanDTO tk)
        {
            string sql = "update TaiKhoan set MatKhau='" + tk.MatKhau + "' where TenTaiKhoan='" + tk.TenTaiKhoan + "'";
            DataProvider.ExcuteQuery(sql);
        }

        public static void suaHoTen(TaiKhoanDTO tk)
        {
            string sql = "update TaiKhoan set HoTen=N'" + tk.HoTen + "' where TenTaiKhoan='" + tk.TenTaiKhoan + "'";
            DataProvider.ExcuteQuery(sql);
        }

        public static void suaNgaySinh(TaiKhoanDTO tk)
        {
            string sql = "update TaiKhoan set NgaySinh='" + tk.NgaySinh.ToShortDateString() + "' where TenTaiKhoan='" + tk.TenTaiKhoan + "'";
            DataProvider.ExcuteQuery(sql);
        }

        public static void suaDiaChi(TaiKhoanDTO tk)
        {
            string sql = "update TaiKhoan set DiaChi=N'" + tk.DiaChi + "' where TenTaiKhoan='" + tk.TenTaiKhoan + "'";
            DataProvider.ExcuteQuery(sql);
        }

        public static void suaChuThe(TaiKhoanDTO tk)
        {
            string sql = "update TaiKhoan set TenChuThe=N'" + tk.TenChuThe + "' where TenTaiKhoan='" + tk.TenTaiKhoan + "'";
            DataProvider.ExcuteQuery(sql);
        }

        public static void suaNgayHH(TaiKhoanDTO tk)
        {
            string sql = "update TaiKhoan set NgayHetHan='" + tk.NgayHetHan.ToShortDateString() + "' where TenTaiKhoan='" + tk.TenTaiKhoan + "'";
            DataProvider.ExcuteQuery(sql);
        }

        public static void suaLoaiThe(TaiKhoanDTO tk)
        {
            string sql = "update TaiKhoan set LoaiTheNganHang='" + tk.LoaiTheNganHang + "' where TenTaiKhoan='" + tk.TenTaiKhoan + "'";
            DataProvider.ExcuteQuery(sql);
        }

        public static void suaMaThe(TaiKhoanDTO tk)
        {
            string sql = "update TaiKhoan set MaThe='" + tk.MaThe + "' where TenTaiKhoan='" + tk.TenTaiKhoan + "'";
            DataProvider.ExcuteQuery(sql);
        }
        public static string laymathe(string tk)
        {
            string sql = "select MaThe from TAIKHOAN Where TenTaiKhoan='"+tk+"'";
            DataTable dt = new DataTable();
            dt = DataProvider.DataTable(sql);

            return dt.Rows[0][0].ToString();
        }

        #region "Một Số Hàm Xử Lí"
        //Mã hóa thẻ theo thuật toán SHA1
        public static string getSHA1Hash(string input)
        {
            // Create a new instance of the MD5CryptoServiceProvider object.
            SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider();

            // Convert the input string to a byte array and compute the hash.
            byte[] data = sha.ComputeHash(Encoding.Default.GetBytes(input));
            //return data;
            //// Create a new Stringbuilder to collect the bytes
            //// and create a string.
            StringBuilder sBuilder = new StringBuilder();

            //// Loop through each byte of the hashed data 
            //// and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            //// Return the hexadecimal string.
            return sBuilder.ToString();
        }

        //Mã hóa mật khẩu theo thuật toán MD5
        public static string getMd5Hash(string input)
        {
            // Create a new instance of the MD5CryptoServiceProvider object.
            MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();

            // Convert the input string to a byte array and compute the hash.
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string.
            return sBuilder.ToString();
        }

        //Kiểm tra mã thẻ tín dụng theo thuật toán Luhn
        public static bool ValidateLuhn(string value)
        {
            int CheckSum = 0;
            bool DoubleFlag = (value.Length % 2 == 0);
            char Digit = '\0';
            int DigitValue = 0;
            foreach (char Digit_loopVariable in value)
            {
                Digit = Digit_loopVariable;
                DigitValue = int.Parse(Digit.ToString());
                if (DoubleFlag)
                {
                    DigitValue *= 2;
                    if (DigitValue > 9)
                    {
                        DigitValue -= 9;
                    }
                }
                CheckSum += DigitValue;
                DoubleFlag = !DoubleFlag;
            }
            return (CheckSum % 10 == 0);
        }
        #endregion
    }
}